package com.wswy.wzcx.ui.car.remind

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.model.Converter
import com.wswy.wzcx.model.car.CarExtendInfo
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.model.reSend
import com.wswy.wzcx.module.UserDataProvider
import com.wswy.wzcx.module.base.BaseViewModel
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.onResult

class RemindViewModel : BaseViewModel() {

    data class InspectionRegDate(var regDate: String? = null, var endDate: String? = null)


    private val companyListLiveData = MutableLiveData<Resource<List<String>>>()
    private val addInsuranceLiveData = MutableLiveData<Resource<CarExtendInfo?>>()
    private val addValuationLiveData = MutableLiveData<Resource<CarExtendInfo?>>()

    private val userCarInfoLiveData = MutableLiveData<UserCarInfo>()

    private val submitJZGLiveData = MutableLiveData<Resource<Any?>>()

    private val inspectionRegLiveData = MutableLiveData<InspectionRegDate>().apply {
        value = InspectionRegDate()
    }

    lateinit var userCarInfo: UserCarInfo

    val addInsuranceStatus = AddInsuranceSaveStatus()
    val addInspectionStatus = AddInspectionSaveStatus()

    val addValuationStatus = AddValuationSaveStatus()


    fun loadCarInfo(carId: Int) {

        addRun(UserDataProvider.get().carFineList(true, carId).firstOrError().onResult({

            it?.run {
                Converter.convert2Result(this).run {
                    //接口查询成功
                    if (isSuccess && userCarInfo != null) {
                        //返回显示
                        setCarInfo(userCarInfo)
                    } else {
                        userCarInfoLiveData.postValue(null)
                    }
                }
            }

        }, {
            userCarInfoLiveData.postValue(null)
        }))

    }


    fun setCarInfo(userCarInfo: UserCarInfo) {
        this.userCarInfo = userCarInfo
        userCarInfoLiveData.postValue(userCarInfo)
    }

    fun setCarExtendInfo(carExtendInfo: CarExtendInfo? = null) {
        this.userCarInfo.carExtendInfo = carExtendInfo

        UserDataProvider.get().updateCarExtendInfo(userCarInfo)

    }

    fun loadCompany() {
        addRun(Api.get().carInsuranceCompany().onResult(companyListLiveData))
    }

    /**
     * 添加保修
     */
    fun addInsurance(date: String?, company: String?) {
        addRun(Api.get().carInsurance(userCarInfo.id, date, company).onResult(addInsuranceLiveData))
    }

    /**
     * 添加年检更新的
     */
    fun addInspection(regDate: String?, validityDate: String?, mannedNum: String?,
                      carType: String?, natureOfUse: String?,
                      inspectReminder: String?) {
        addRun(Api.get().carInspection(userCarInfo.id, regDate, validityDate, mannedNum, carType, natureOfUse, inspectReminder).onResult(addInsuranceLiveData))
    }


//    /**
//     * 添加年检
//     */
//    fun addInspection(regDate: String?, validityDate: String?) {
//        addRun(Api.get().carInspection(userCarInfo.id, regDate, validityDate).onResult(addInsuranceLiveData))
//    }



    /**
     * 添加估值
     */
    fun addValuation() {
        addRun(Api.get().carValuation(userCarInfo.id,
                addValuationStatus.carModelId,
                addValuationStatus.regDate,
                addValuationStatus.drivenDistance,
                addValuationStatus.regCityId).onResult(addValuationLiveData))
    }

    fun submitClue() {
        addRun(Api.get().jzgClues(userCarInfo.id).onResult(submitJZGLiveData))
    }

    fun calInspectionData(regDate: String?) {

        inspectionRegLiveData.value?.regDate = regDate

        addRun(Api.get().inspectionEndTime(regDate).onResult({

            inspectionRegLiveData.value?.endDate = it

            inspectionRegLiveData.reSend()

        }, {

        }))
    }

    fun getUserCarInfoLiveData(): LiveData<UserCarInfo> = userCarInfoLiveData

    /**
     * 保险等信息变化
     */
    fun getAddInsuranceLiveData(): LiveData<Resource<CarExtendInfo?>> = addInsuranceLiveData

    fun getAddValuationLiveData(): LiveData<Resource<CarExtendInfo?>> = addValuationLiveData


    fun getSubmitClueLiveData(): LiveData<Resource<Any?>> = submitJZGLiveData

    fun getCalInspectionDate(): LiveData<InspectionRegDate> = inspectionRegLiveData

    /**
     * 保险公司列表
     */
    fun getCompanyList(): Array<String>? {
        return companyListLiveData.value?.data?.toTypedArray()
    }

    fun addInsuranceSaveChoose(year: Int = -1, month: Int = -1, dayOfMonth: Int = -1, company: Int = -1) {
        if (year != -1) {
            addInsuranceStatus.year = year
        }
        if (month != -1) {
            addInsuranceStatus.month = month
        }
        if (dayOfMonth != -1) {
            addInsuranceStatus.dayOfMonth = dayOfMonth
        }
        if (company != -1) {
            addInsuranceStatus.company = company
        }
    }

    fun addInspectionSaveChoose(regDate: String? = null, validityDate: String? = null) {
        if (regDate != null) {
            addInspectionStatus.regDate = regDate
        }
        if (validityDate != null) {
            addInspectionStatus.validityDate = validityDate
        }
    }

    fun addValuationSaveChoose(carModelId: String? = null,
                               carModelName: String? = null,
                               regDate: String? = null,
                               drivenDistance: String? = null,
                               regCityId: String? = null,
                               regCityName: String? = null) {
        carModelId?.run {
            addValuationStatus.carModelId = this
        }
        carModelName?.run {
            addValuationStatus.carModelName = this
        }
        regDate?.run {
            addValuationStatus.regDate = this
        }
        drivenDistance?.run {
            addValuationStatus.drivenDistance = this
        }
        regCityId?.run {
            addValuationStatus.regCityId = this
        }
        regCityName?.run {
            addValuationStatus.regCityName = this
        }
    }

    data class AddInsuranceSaveStatus(var year: Int = 2018, var month: Int = 0, var dayOfMonth: Int = 1, var company: Int = -1)

    data class AddInspectionSaveStatus(var regDate: String? = null, var validityDate: String? = null) {
        fun getYMD(date: String?): Array<Int>? {
            return date?.split("-")?.mapNotNull { it.toIntOrNull() }?.toTypedArray()?.let {
                if (it.size == 3) {
                    it
                } else {
                    null
                }
            }
        }
    }

    data class AddValuationSaveStatus(var carModelId: String? = null,
                                      var carModelName: String? = null,
                                      var regDate: String? = null,
                                      var drivenDistance: String? = null,
                                      var regCityId: String? = null,
                                      var regCityName: String? = null) {


        fun getYMD(): Array<Int>? {
            return regDate?.split("-")?.mapNotNull { it.toIntOrNull() }?.toTypedArray()?.let {
                if (it.size == 3) {
                    it
                } else {
                    null
                }
            }
        }

    }

}